<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class zzcon extends check{
	//构造函数
	public function __construct(){
		parent::__construct();
		$arr = array();
		$zcrow = spClass('m_zzcat')->spLinker()->findAll(null,'odnum ASC,id ASC');
		foreach($zcrow as $v){
			$arr[] = array('id'=>$v['id'],'pid'=>$v['pid'],'catename'=>$v['catename'],'odnum'=>$v['odnum'],'modelname'=>$v['model']['modelname']);
		}
		$treeObj = spClass('Tree',array(array('id','pid','catename','format')));
		$this->zzcat = $treeObj->getTree($arr);
	}
	//文档列表
	public function lis(){
		if(!$cid = intval($this->spArgs('cid')))	$this->error('缺少必要参数！',spUrl('zzcat','lis'));
		$zzcat = spClass('m_zzcat')->find(array('id'=>$cid));
		$modelid = $zzcat['modelid'];
		$table = spClass('m_table')->find(array('modelid'=>$modelid));
		$tablename = $GLOBALS['G_SP']['db']['prefix'] . $table['tablename'];
		$where = ' 1 = 1 ';
		if($title = trim($this->spArgs('title'))){
			$title_s = spClass('spModel')->escape('%' . $title . '%');
			$where .= " AND title LIKE {$title_s} ";
			$this->title = $title;
		}
		$where .= " AND cid = {$cid} ";
		$this->cid = $cid;
		$this->catename = $zzcat['catename'];
		$this->tablename = $tablename;
		$page = (int) $this->spArgs('page',1) > 1 ? (int) $this->spArgs('page',1) : 1;
		$this->zzcon = spDB($tablename,'id')->spPager($page,15)->findAll($where,'id DESC');
		$this->pager = spDB($tablename,'id')->spPager()->getPager();
		$this->display('zzcon_lis.tpl.php');
	}
	//添加文档
	public function add(){
		if(!$cid = intval($this->spArgs('cid')))	$this->error('缺少必要参数！',spUrl('zzcat','lis'));
		$this->cid = $cid;
		$zzcat = spClass('m_zzcat')->find(array('id'=>$cid));
		$modelid = $zzcat['modelid'];
		$diyfield = spClass('m_field')->findAll(array('modelid'=>$modelid,'canupd'=>1),'odnum ASC,fieldid ASC');
		$this->_mktpl($diyfield);
		$this->display('zzcon_add.tpl.php');
	}
	public function doadd(){
		if(!$cid = intval($this->spArgs('cid')))	$this->error('缺少必要参数！',spUrl('zzcat','lis'));
		$zzcat = spClass('m_zzcat')->find(array('id'=>$cid));
		$modelid = $zzcat['modelid'];
		$table = spClass('m_table')->find(array('modelid'=>$modelid));
		$tablename = $GLOBALS['G_SP']['db']['prefix'] . $table['tablename'];
		$rows = $this->spArgs();
		if(!$rows['visible'])	$rows['visible'] = 0;
		if(!$rows['stick'])	$rows['stick'] = 0;
		$rows['addtime'] = time();
		if(!empty($rows['is_time'])){
			foreach($rows['is_time'] as $v){
				$rows[$v] = strtotime($rows[$v]);
			}
		}
		if(!empty($rows['is_checkbox'])){
			foreach($rows['is_checkbox'] as $v){
				$rowstr = '|';
				foreach ($rows[$v] as $v2){
					$rowstr .= $v2 . '|';
				}
				$rows[$v] = trim($rowstr,'|');
			}
		}
		if(!$insertid = spDB($tablename,'id')->create($rows)){
			$this->error('添加文档失败！',spUrl('zzcon','lis',array('cid'=>$cid)));
		}		
		if(!empty($rows['album'])){
 			foreach($rows['album'] as $v){
 				if(!empty($v)){
 					$thumb = explode('/', $v);
 					$thumb = $thumb[0] . '/' . $thumb[1] . '/' . $thumb[2] . '/thumb_' . $thumb[3];
 					spClass('m_zzpic')->create(array('zid'=>$insertid,'zcid'=>$cid,'photo'=>$v,'thumb'=>$thumb));
 				}
 			}
 		}
		$this->success('添加文档成功！',spUrl('zzcon','lis',array('cid'=>$cid)));
	}
	//更新文档
	public function upd(){
		if((!$id = intval($this->spArgs('id'))) || (!$cid = intval($this->spArgs('cid')))){
			$this->error('缺少必要参数！',spUrl('zzcat','lis'));
		}
		$this->cid = $cid;
		$zzcat = spClass('m_zzcat')->find(array('id'=>$cid));
		$modelid = $zzcat['modelid'];
		$table = spClass('m_table')->find(array('modelid'=>$modelid));
		$tablename = $GLOBALS['G_SP']['db']['prefix'] . $table['tablename'];
		$zzcon = spDB($tablename,'id')->find(array('id'=>$id));
		$diyfield = spClass('m_field')->findAll(array('modelid'=>$modelid,'canupd'=>1),'odnum ASC,fieldid ASC');
		$this->_mktpl($diyfield,$zzcon);
		$this->zzcon = $zzcon;
		$this->zzpic = spClass('m_zzpic')->findAll(array('zid'=>$id,'zcid'=>$cid),'id DESC');
		$this->display('zzcon_upd.tpl.php');
	}
	public function doupd(){
		if((!$id = intval($this->spArgs('id'))) || (!$cid = intval($this->spArgs('cid')))){
			$this->error('缺少必要参数！',spUrl('zzcat','lis'));
		}
		$zzcat = spClass('m_zzcat')->find(array('id'=>$cid));
		$modelid = $zzcat['modelid'];
		$table = spClass('m_table')->find(array('modelid'=>$modelid));
		$tablename = $GLOBALS['G_SP']['db']['prefix'] . $table['tablename'];
		$rows = $this->spArgs();
		if(!$rows['visible'])	$rows['visible'] = 0;
		if(!$rows['stick'])	$rows['stick'] = 0;
		if(!empty($rows['is_time'])){
			foreach($rows['is_time'] as $v){
				$rows[$v] = strtotime($rows[$v]);
			}
		}
		if(!empty($rows['is_checkbox'])){
			foreach($rows['is_checkbox'] as $v){
				$rowstr = '|';
				foreach ($rows[$v] as $v2){
					$rowstr .= $v2 . '|';
				}
				$rows[$v] = trim($rowstr,'|');
			}
		}
		$thumb = spDB($tablename,'id')->find(array('id'=>$id),null,'thumb');
		if(!empty($thumb['thumb']) && !empty($rows['thumb']) && $thumb['thumb']!=$rows['thumb']){
			$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
			if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
			@unlink($thumb['thumb']);
			spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
		}
		if(false == spDB($tablename,'id')->update(array('id'=>$id),$rows)){
			$this->error('更新文档失败！',spUrl('zzcon','lis',array('cid'=>$cid)));
		}
		if(!empty($rows['album'])){
 			foreach($rows['album'] as $v){
 				if(!empty($v)){
 					$thumb = explode('/', $v);
 					$thumb = $thumb[0] . '/' . $thumb[1] . '/' . $thumb[2] . '/thumb_' . $thumb[3];
 					spClass('m_zzpic')->create(array('zid'=>$id,'zcid'=>$cid,'photo'=>$v,'thumb'=>$thumb));
 				}
 			}
 		}
		$this->success('更新文档成功！',spUrl('zzcon','lis',array('cid'=>$cid)));
	}
	//删除文档
	public function del(){
		if((!$id = intval($this->spArgs('id'))) || (!$cid = intval($this->spArgs('cid')))){
			$this->error('缺少必要参数！',spUrl('zzcat','lis'));
		}
		$zzcat = spClass('m_zzcat')->find(array('id'=>$cid));
		$modelid = $zzcat['modelid'];
		$table = spClass('m_table')->find(array('modelid'=>$modelid));
		$tablename = $GLOBALS['G_SP']['db']['prefix'] . $table['tablename'];
		$zzpic = spClass('m_zzpic')->findAll(array('zid'=>$id,'zcid'=>$cid));
		for($i=0;$i<count($zzpic);$i++){
			if(!empty($zzpic[$i]['photo'])){
				$photorow = spClass('m_thumb')->find(array('photo'=>$zzpic[$i]['photo']));
				if(!empty($photorow['thumb'])) @unlink($photorow['thumb']);
				@unlink($zzpic[$i]['photo']);
				spClass('m_thumb')->delete(array('id'=>$photorow['id']));
			}
		}
		spClass('m_zzpic')->delete(array('zid'=>$id,'zcid'=>$cid));
		$thumb = spDB($tablename,'id')->find(array('id'=>$id),null,'thumb');
		if(!empty($thumb['thumb'])){
			if(strpos($thumb['thumb'], 'thumb_') !== false){
				$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
			}else{
				$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['thumb']));
			}
			if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
			if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
			spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
		}
		if(false != spDB($tablename,'id')->delete(array('id'=>$id))){
			$this->success('删除文档成功！',spUrl('zzcon','lis',array('cid'=>$cid)));
		}else{
			$this->error('删除文档失败！',spUrl('zzcon','lis',array('cid'=>$cid)));
		}
	}
	//批量删除
	public function pldel(){
		if(!$idrow = $this->spArgs('id'))	$this->error('请选择要删除的文章编号！');
		$tablename = trim($this->spArgs('tablename'));
		$cid = trim($this->spArgs('cid'));
		for($i=0;$i<count($idrow);$i++){
			$zzpic = spClass('m_zzpic')->findAll(array('zid'=>$idrow[$i],'zcid'=>$cid));
			for($j=0;$j<count($zzpic);$j++){
				if(!empty($zzpic[$j]['photo'])){
					$photorow = spClass('m_thumb')->find(array('photo'=>$zzpic[$j]['photo']));
					if(!empty($photorow['thumb'])) @unlink($photorow['thumb']);
					@unlink($zzpic[$j]['photo']);
					spClass('m_thumb')->delete(array('id'=>$photorow['id']));
				}
			}
			spClass('m_zzpic')->delete(array('zid'=>$idrow[$i]));
			$thumb = spDB($tablename,'id')->find(array('id'=>$idrow[$i]),null,'thumb');
			if(!empty($thumb['thumb'])){
				if(strpos($thumb['thumb'], 'thumb_') !== false){
					$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
				}else{
					$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['thumb']));
				}
				if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
				if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
				spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
			}
			spDB($tablename,'id')->delete(array('id'=>$idrow[$i]));
		}
		$this->success('批量删除文档成功！',spUrl('zzcon','lis',array('cid'=>$cid)));
	}
	//删除单张图片
	public function delpic(){
		if(!$id = intval($this->spArgs('id')))	exit('0');
		$thumb = spClass('m_zzpic')->find(array('id'=>$id),null,'photo');
		if(!empty($thumb['photo'])){
			$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['photo']),null,'id,thumb');
			if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
			@unlink($thumb['photo']);
			spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
		}
		if(false != spClass('m_zzpic')->delete(array('id'=>$id)))	exit('1');
	}
	//更新排序
	public function updod(){
		$tablename = trim($this->spArgs('tablename'));
		$cid = trim($this->spArgs('cid'));
		$idrow = $this->spArgs('idrow');
		$odnum = $this->spArgs('odnum');
		for($i=0;$i<count($idrow);$i++){
			spDB($tablename,'id')->update(array('id'=>$idrow[$i]),array('odnum'=>(int)$odnum[$i]));
		}
		$this->success('更新排序成功！',spUrl('zzcon','lis',array('cid'=>$cid)));
	}
	//更新浏览
	public function updvs(){
		$tablename = trim($this->spArgs('tablename'));
		$cid = trim($this->spArgs('cid'));
		$idrow = $this->spArgs('idrow');
		$views = $this->spArgs('views');
		for($i=0;$i<count($idrow);$i++){
			spDB($tablename,'id')->update(array('id'=>$idrow[$i]),array('views'=>(int)$views[$i]));
		}
		$this->success('更新浏览成功！',spUrl('zzcon','lis',array('cid'=>$cid)));
	}
	//组装模板
	private function _mktpl($fieldrow = array(),$updv = array()){
		if(empty($fieldrow))	return false;
		$type_1_7 = $type_8 = $type_9_10 = array();
		foreach($fieldrow as $v){
			global $__action;
			if($__action == 'upd' && $v['typeid'] != 5 && $v['typeid'] != 6){
				$v['value'] = $updv[$v['field']];
			}
			switch($v['typeid']){
				case 1:
					$html = <<<EOT1
<tr>
	<td class="txt_right txt_s_blue">{$v['name']}：</td>
	<td>
		<input name="{$v['field']}" value="{$v['value']}" type="text" class="blue_input input_w_250 f_left"  />
		<span><strong style='color:red;'>数字类型|[0-255]</strong></span>
	</td>
</tr>
EOT1;
					$type_1_7[] = $html;
					break;
				case 2:
					$html = <<<EOT2
<tr>
	<td class="txt_right txt_s_blue">{$v['name']}：</td>
	<td>
		<input name="{$v['field']}" value="{$v['value']}" type="text" class="blue_input input_w_250 f_left"  />
		<span><strong style='color:red;'>数字类型|[0-65000]</strong></span>
	</td>
</tr>
EOT2;
					$type_1_7[] = $html;
					break;
				case 3:
					$html = <<<EOT3
<tr>
	<td class="txt_right txt_s_blue">{$v['name']}：</td>
	<td>
		<input name="{$v['field']}" value="{$v['value']}" type="text" class="blue_input input_w_250 f_left"  />
		<span><strong style='color:red;'>数字类型|[65000+]</strong></span>
	</td>
</tr>
EOT3;
					$type_1_7[] = $html;
					break;
				case 4:
					$time = ($__action == 'upd') ? date('Y-m-d H:i:s',$v['value']) : date('Y-m-d H:i:s');
					$html = <<<EOT4
<tr>
	<td class="txt_right txt_s_blue">{$v['name']}：</td>
	<td>
		<input type="hidden" name="is_time[]" value="{$v['field']}" />
		<input id="{$v['field']}" name="{$v['field']}" type="text" readonly value="{$time}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="blue_input input_w_175" />
	</td>
</tr>
EOT4;
					$type_1_7[] = $html;
					break;
				case 5:
					$valuerow = explode('|',$v['value']);
					$selected = $updv[$v['field']];
					$html_1 = '';
					foreach($valuerow as $vr){
						if($vr == $selected){
							$html_1 .= '<input class="m_lr_5" type="radio" checked value="'. $vr .'" name="' . $v['field'] . '"><label>' . $vr . '</label>';
						}else{
							$html_1 .= '<input class="m_lr_5" type="radio" value="'. $vr .'" name="' . $v['field'] . '"><label>' . $vr . '</label>';
						}
					}
					$html = <<<EOT5
<tr>
	<td class="txt_right txt_s_blue">{$v['name']}：</td>
	<td>{$html_1}</td>
</tr>
EOT5;
					$type_1_7[] = $html;
					break;
				case 6:
					$valuerow = explode('|',$v['value']);
					$selected = explode('|',$updv[$v['field']]);
					$html_1 = '';
					foreach($valuerow as $vr){
						if(in_array($vr,$selected)){
							$html_1 .= '<input type="checkbox" checked value="'. $vr .'" name="' . $v['field'] . '[]" class="m_lr_5"/><label>' . $vr . '</label>';
						}else{
							$html_1 .= '<input type="checkbox" value="'. $vr .'" name="' . $v['field'] . '[]" class="m_lr_5"/><label>' . $vr . '</label>';
						}
					}
					$html = <<<EOT6
<tr>
	<td class="txt_right txt_s_blue">{$v['name']}：</td>
	<td><input type="hidden" name="is_checkbox[]" value="{$v['field']}" />{$html_1}</td>
</tr>
EOT6;
					$type_1_7[] = $html;
					break;
				case 7:
					$html = <<<EOT7
<tr>
	<td class="txt_right txt_s_blue">{$v['name']}：</td>
	<td><input name="{$v['field']}" value="{$v['value']}" type="text" class="blue_input input_w_250 f_left"  /></td>
</tr>
EOT7;
					$type_1_7[] = $html;
					break;
				case 8:
					$html = <<<EOT8
<tr>
	<td class="txt_right txt_s_blue" valign="top">{$v['name']}：</td>
	<td><textarea name="{$v['field']}" cols="60" rows="5" class="blue_textarea">{$v['value']}</textarea></td>
</tr>
EOT8;
					$type_8[] = $html;
					break;
				case 9:
					$html = <<<EOT9
<script type="text/javascript">
KindEditor.ready(function(K){
	var {$v['field']} = K.create('textarea[name="{$v['field']}"]', {
		themeType : 'simple',
		resizeType : 1,
		allowImageUpload : false,
		items : [
			'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
			'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
			'insertunorderedlist', '|', 'image', 'link']
	});
});
</script>
<tr>
	<td class="txt_right txt_s_blue" valign="top">{$v['name']}：</td>
	<td><textarea name="{$v['field']}" style="width:700px;height:350px;visibility:hidden;">{$v['value']}</textarea></td>
</tr>
EOT9;
					$type_9_10[] = $html;
					break;
				case 10:
					$html = <<<EOT10
<script type="text/javascript">
KindEditor.ready(function(K){
	var {$v['field']} = K.create('textarea[name="{$v['field']}"]', {
		themeType : 'simple',
		resizeType : 1,
		uploadJson : 'common/KEditor/upload_json.php',
		fileManagerJson : 'common/KEditor/file_manager_json.php',
		allowFileManager : true,
	});
});
</script>
<tr>
	<td class="txt_right txt_s_blue" valign="top">{$v['name']}：</td>
	<td><textarea name="{$v['field']}" style="width:700px;height:350px;visibility:hidden;">{$v['value']}</textarea></td>
</tr>
EOT10;
					$type_9_10[] = $html;
					break;
			}
		}
		$this->type_1_7 = $type_1_7;
		$this->type_8 = $type_8;
		$this->type_9_10 = $type_9_10;
	}
}
